package com.zicheng.util;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.regex.Pattern;

public class RenamePNGFiles {
    // 定义正则表达式模式，用于匹配文件名
    private static final Pattern FILENAME_PATTERN = Pattern.compile("(\\d+)@2x\\.png");

    public static void main(String[] args) {
        // 请将此路径替换为实际包含 PNG 图片的文件夹路径
        String folderPath = "C:\\Users\\12464\\Desktop\\lemon玩家等级切图";
        Path folder = Paths.get(folderPath);

        if (Files.exists(folder) && Files.isDirectory(folder)) {
            try {
                // 列出文件夹中的所有文件并进行流处理
                Files.list(folder)
                        .filter(path -> path.toString().toLowerCase().endsWith(".png"))
                        .map(Path::toFile)
                        .forEach(RenamePNGFiles::processFile);
            } catch (IOException e) {
                System.err.println("读取文件夹时出现错误: " + e.getMessage());
            }
        }
    }

    private static void processFile(File file) {
        String oldName = file.getName();
        java.util.regex.Matcher matcher = FILENAME_PATTERN.matcher(oldName);
        if (matcher.matches()) {
            int number = Integer.parseInt(matcher.group(1));
            String newName = getNewName(number);
            if (newName != null) {
                File newFile = new File(file.getParentFile(), newName);
                if (file.renameTo(newFile)) {
                    System.out.println("已将 " + oldName + " 重命名为 " + newName);
                } else {
                    System.err.println("重命名 " + oldName + " 失败");
                }
            }
        }
    }

    private static String getNewName(int number) {
        if (number >= 11 && number <= 40) {
            return (number - 10) + ".png";
        } else if (number >= 51) {
            return (number - 20) + ".png";
        }
        return null;
    }
} 